package com.wl.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wl.common.exception.Assert;
import com.wl.common.result.ResponseEnum;
import com.wl.common.util.MD5;
import com.wl.srb.base.util.JwtUtils;
import com.wl.srb.core.mapper.UserAccountMapper;
import com.wl.srb.core.mapper.UserLoginRecordMapper;
import com.wl.srb.core.pojo.entity.UserAccount;
import com.wl.srb.core.pojo.entity.UserInfo;
import com.wl.srb.core.mapper.UserInfoMapper;
import com.wl.srb.core.pojo.entity.UserLoginRecord;
import com.wl.srb.core.pojo.vo.LoginVO;
import com.wl.srb.core.pojo.vo.RegisterVO;
import com.wl.srb.core.pojo.vo.UserInfoVO;
import com.wl.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author Helen
 * @since 2021-06-26
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    private UserAccountMapper userAccountMapper;
    @Autowired
    private UserLoginRecordMapper userLoginRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void register(RegisterVO registerVO) {
        //判断用户是否已被注册
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.lambda().eq(UserInfo::getMobile,registerVO.getMobile());
        int count = this.count(userInfoQueryWrapper);
        Assert.isTrue(count==0, ResponseEnum.MOBILE_EXIST_ERROR);
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(registerVO,userInfo);
        userInfo.setPassword(MD5.encrypt(registerVO.getPassword()));
        userInfo.setStatus(UserInfo.STAUS_LOCKED);
        this.save(userInfo);

        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userInfo.getId());
        userAccountMapper.insert(userAccount);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserInfoVO login(LoginVO loginVO, String ip) {
        String mobile = loginVO.getMobile();
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.lambda().eq(UserInfo::getMobile,mobile)
                .eq(UserInfo::getUserType,loginVO.getUserType());
        UserInfo userInfo = this.getOne(userInfoQueryWrapper);
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);
        Assert.equals(MD5.encrypt(loginVO.getPassword()),userInfo.getPassword(),ResponseEnum.LOGIN_PASSWORD_ERROR);
        Assert.equals(userInfo.getStatus(),UserInfo.STAUS_NORMAL,ResponseEnum.LOGIN_LOKED_ERROR);
        UserLoginRecord userLoginRecord = new UserLoginRecord();
        userLoginRecord.setUserId(userInfo.getId());
        userLoginRecord.setIp(ip);
        userLoginRecordMapper.insert(userLoginRecord);
        String token = JwtUtils.createToken(userInfo.getId(), userInfo.getName());
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userInfo,userInfoVO);
        userInfoVO.setToken(token);

        return userInfoVO;
    }
}
